package com.anti.modular.owner.ownerleaseroom.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.busroom.service.BusRoomService;
import com.anti.modular.owner.ownerlease.param.OwerRoom;
import com.anti.modular.owner.ownerleaseroom.entity.OwnerLeaseRoom;
import com.anti.modular.owner.ownerleaseroom.enums.OwnerLeaseRoomExceptionEnum;
import com.anti.modular.owner.ownerleaseroom.param.OwnerLeaseRoomParam;
import com.anti.modular.owner.ownerleaseroom.service.OwnerLeaseRoomService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 業主房間關係表service接口实现类
 *
 * @author anti
 * @date 2021-05-20 14:28:47
 */
@Service
public class OwnerLeaseRoomServiceImpl implements OwnerLeaseRoomService {
    @Resource
    DBUtils dbUtils;
    @Resource
    BusRoomService busRoomService;

    @Override
    public OwnerLeaseRoom getById(OwnerLeaseRoomParam ownerLeaseRoomParam){
        String sql="select * from owner_lease_room where id =? ";
        return dbUtils.get(sql,OwnerLeaseRoom.class,ownerLeaseRoomParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,OwnerLeaseRoomParam ownerLeaseRoomParam) {
        StringBuilder sb=new StringBuilder("select * from owner_lease_room where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(ownerLeaseRoomParam)) {
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<OwnerLeaseRoom> list(OwnerLeaseRoomParam ownerLeaseRoomParam) {
        String sql="";
        return dbUtils.query(sql,OwnerLeaseRoom.class);
    }

    @Override
    public List<OwnerLeaseRoom> getRoomNamesByOwnerId(Long leaseId) {
        String sql="select * from owner_lease_room where owner_lease_id=? ";
        return dbUtils.query(sql,OwnerLeaseRoom.class,leaseId);
    }

    @Override
    public void add(OwnerLeaseRoomParam ownerLeaseRoomParam) {
        OwnerLeaseRoom ownerLeaseRoom = new OwnerLeaseRoom();
        BeanUtil.copyProperties(ownerLeaseRoomParam, ownerLeaseRoom);
        dbUtils.insert(ownerLeaseRoom);
    }

    @Override
    public void addByList(List<OwerRoom> owerRooms, Long ownerLeaseId) {
        OwnerLeaseRoom ownerLeaseRoom = null;
        OwerRoom owerRoom = null;
        for (int i = 0; i < owerRooms.size(); i++) {
            owerRoom = owerRooms.get(i);
            ownerLeaseRoom = new OwnerLeaseRoom();
            ownerLeaseRoom.setOwnerLeaseId(ownerLeaseId);
            ownerLeaseRoom.setRoomId(owerRoom.getId());
            ownerLeaseRoom.setRoomName(owerRoom.getName());
            dbUtils.insert(ownerLeaseRoom);
            busRoomService.editByOwnerZy(owerRoom.getId(),ownerLeaseId);
        }

    }

    public void delete(OwnerLeaseRoomParam ownerLeaseRoomParam) {
        String sql="delete  from owner_lease_room where id =? ";
        dbUtils.execSql(sql,ownerLeaseRoomParam.getId());
    }

    public void edit(OwnerLeaseRoomParam ownerLeaseRoomParam) {
        OwnerLeaseRoom ownerLeaseRoom = this.queryOwnerLeaseRoom(ownerLeaseRoomParam);
        BeanUtil.copyProperties(ownerLeaseRoomParam, ownerLeaseRoom);
        dbUtils.update(ownerLeaseRoom);
    }

    @Override
    public OwnerLeaseRoom detail(OwnerLeaseRoomParam ownerLeaseRoomParam) {
        return this.queryOwnerLeaseRoom(ownerLeaseRoomParam);
    }

    /**
     * 获取業主房間關係表
     *
     * @author anti
     * @date 2021-05-20 14:28:47
     */
    private OwnerLeaseRoom queryOwnerLeaseRoom(OwnerLeaseRoomParam ownerLeaseRoomParam) {
        OwnerLeaseRoom ownerLeaseRoom = this.getById(ownerLeaseRoomParam);
        if (ObjectUtil.isNull(ownerLeaseRoom)) {
            throw new BusinessException(OwnerLeaseRoomExceptionEnum.NOT_EXIST);
        }
        return ownerLeaseRoom;
    }


}
